package com.sss.hloster06;

import java.util.Scanner;

public class JavaExercise29 {

	public static void main(String[] args) {
		test29();
	}

	/**
	 * 求一个3*3矩阵对角线元素之和
	 */
	private static void test29() {
		int i = 3;
		int j = 3;
		int[][] arr = new int[i][j];
		System.out.println("请输入3*3矩阵：");
		Scanner sc = new Scanner(System.in);
		for (int m = 0; m < i; m++) {
			for (int n = 0; n < j; n++) {
				System.out.print("第一行列式的" + (m + 1) + "行，第" + (n + 1) + "列为：");
				arr[m][n] = sc.nextInt();
			}
		}
		System.out.println("此矩阵为：");
		display(arr);
		sum(arr);
	}

	public static void display(int[][] h) {
		for (int m = 0; m < h.length; m++) {
			for (int n = 0; n < h[m].length; n++) {
				System.out.print(h[m][n]);
				System.out.print("  ");
			}
			System.out.println();
		}
	}

	public static void sum(int[][] a) {
		int s = 0;int w = 0;
		for (int m = 0; m < a.length; m++) {
			for (int n = 0; n < a[m].length; n++) {
				if (m == n) {
					
					s += a[m][n];
					
				}
				if (m + n == 2) {
					
					w += a[m][n];
					
				}
			}
		}
		System.out.println("主对角线元素之和为：" + s);
		System.out.println("副对角线元素之和为：" + w);
	}
}
